<?php
/**
 * Created by PhpStorm.
 * User: jurentech
 * Date: 2019-06-17
 * Time: 14:49
 */

namespace app\master\controller;

class Son extends Base
{
    public function index()
    {
        return $this->fetch();
    }

    public function chongzhi()
    {
        if(request()->isPost()){
            $res=input('post.');
            $res['type']=1;
            $res['create_time']=time();
            $res['status']='平台主动充值';
            $son=db('son')->where(['son_id'=>$res['son_id']])->value('fee');
            $son=$son+$res['fee'];
            db('son')->where(['son_id'=>$res['son_id']])->update(['fee'=>$son]);
            db('fee_move')->insert($res);
//            var_dump($res);
//            exit();
        }
        $id=input('id');
        $data=db('son')->where(['son_id'=>$id])->find();
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function getsonfee()
    {
        $id=input('son_id');
        $page['page'] = input("page");
        $page['limit'] = input("limit");
        $result['data'] = db('fee_move')
            ->where(['son_id' =>$id])
            ->order('create_time DESC')
            ->page($page['page'], $page['limit'])
            ->select();
        foreach ($result['data'] as $v=>$k){
            $result['data'][$v]['create_time'] = date('Y-m-d H:i:s', $result['data'][$v]['create_time']);
        }
        $result['count'] = db('fee_move')->where(['son_id' =>$id])->count();
        return $this->_table_return(0, "", $result['count'], $result['data']);
    }

    public function addson()
    {
        if(request()->isPost()){
            $data['son_name']=input('post.name');
            $data['password']=input('post.password');
            $data['area_id']=input('post.shi');
            $data['type']=input('post.type');
            $hava=db('son')->where(['son_name'=>$data['son_name']])->count();
            if($hava>=1){
                $this->_ajax_return(602,'账号已存在');
            }
            db('son')->insert($data);
            $this->_ajax_return(200,'添加成功','index');
        }
        $area1=db('area')->where(['pid'=>0])->select();
        $this->assign('area',$area1);
        return $this->fetch();
    }

    public function order()
    {
        $id=input('id');
        $this->assign('id',$id);
        return $this->fetch();
    }

    public function getarea()
    {
        $area=input('area_id');
        $data=db('area')->where(['pid'=>$area])->select();
        $this->_ajax_return(200,'成功','',$data);
    }

    public function getorder()
    {

        $id=input('id');
        $page['page'] = input("page");
        $page['limit'] = input("limit");
        $result['data'] = db('order')
            ->where(['son_id' =>$id,'status'=>1])
            ->order('create_time DESC')
            ->page($page['page'], $page['limit'])
            ->select();
        foreach ($result['data'] as $v=>$k){
            $result['data'][$v]['area_name']=db('area')->where(['area_id'=>$result['data'][$v]['area_id']])->value('area_name');
            $result['data'][$v]['create_time'] = date('Y-m-d H:i:s', $result['data'][$v]['create_time']);
        }
        $result['count'] = db('order')->where(['son_id' =>$id,'status'=>1])->count();
        return $this->_table_return(0, "", $result['count'], $result['data']);
    }

    public function getlist()
    {
        $page['page'] = input("page");
        $page['limit'] = input("limit");
        $result['data'] = db('son')
            ->where(['status' => ['neq', 3]])
            ->order('son_id DESC')
            ->page($page['page'], $page['limit'])
            ->select();
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;

        foreach ($result['data'] as $v=>$k){
            $result['data'][$v]['area_name']=db('area')->where(['area_id'=>$result['data'][$v]['area_id']])->value('area_name');

            $result['data'][$v]['day']=db('order')->where(['status'=>1,'son_id'=>$result['data'][$v]['son_id'],'create_time'=>['between',[$beginToday,$endToday]]])->count();
        }
        $result['count'] = db('son')->where(['status' => ['neq', 3]])->count();
        return $this->_table_return(0, "", $result['count'], $result['data']);
    }

    public function hdelete()
    {
            $id = input('id');
            db('son')->where(['son_id' => $id])->update(['status' => 3]);
            return $this->_ajax_return(200, '成功');
    }

    public function changestatus()
    {
        $id = input('id');
        db('son')->where(['son_id' => $id])->update(['status' => 1]);
        return $this->_ajax_return(200, '成功');
    }

    public function jieshu()
    {
        $id = input('id');
        db('son')->where(['son_id' => $id])->update(['status' => 2]);
        return $this->_ajax_return(200, '成功');
    }


}